/**
* @Author: 李晨光
* @Date: 2019/12/4
* @Version: 1.0
* @Last Modified by: 李晨光
* @Last Modified time: 2019/12/4
**/
<comment>
  组织选择
  属性：

  # 使用方法
  <User ref="User" v-model="User" @change="orgChange"></User>
  methods: {

  值变化
  orgChange(ev) {
    console.log(ev)
  }
  },
  v-model [{label: '1',
  value: '1',
  disabled: false,
  checked: false,
  hasChecked: true,
  hasChildren: true
  }]

</comment>
<template>
  <div id="User">
    <Dialog ref="orgPop" :title="title" :open="open" @close="closeOrgDialog">
      <Select v-if="open" :lists='lists' :multiple="multiple" ref="userSelect" @confirm="confirm" v-model="checked" @expand="expand"
          :dataScope="dataScope" :contactType="contactType" slot="dialog-content" ></Select>
    </Dialog>
  </div>
</template>

<script>

  import Dialog from "@/dialog/Dialog.vue"
  import Select from "@/select/user/UserTreeThrough.vue"

  export default {
    name: 'User',
    components: {
      Select,
      Dialog
    },
    props: {
      orgType: {
        type: String,  // allOrg allOrgProject  currOrg currOrgProject
        default: 'currOrgProject'
      },
      // all---所有人
      // org---组织下所有人
      // project---项目下所有人
      // contact---参建单位下所有人 如果选择
      // org-project---组织+项目所有人
      // project-contact---项目+参建单位所有人
      // org-project-contact---组织+项目+参建单位所有人
      // 如果类型为 contact project-contact org-project-contact  需要配置 contactType
      dataScope: {
        type: String,
        default: 'all'
      },
      // {label: '分包单位', value: '1'},
      // {label: '业主单位', value: '2'},
      // {label: '监理单位', value: '3'},
      // 例 '1,2'  分包单位+ 业主单位
      contactType: {
        type: String,  //  '1,2,3'
        default: '1,2,3'
      },
      title: {
        type: String,
        default: '选择人员'
      },
      // 是否多选
      multiple: {
        type: Boolean,
        default: true
      },
      // 选择结果
      checked: {
        type: Array,
        default: () => {
          return []
        }
      },
      open: {
        type: Boolean,
        default: false
      },
      customRoot: {
        type: Array,
        default: () => {
          return [];
        }
      }
    },
    model: {
      prop: "checked",
      event: "change"
    },
    data() {
      return {
        api: {
          getOrgTreeDataApi: "/safety/getOrgs", // 获取基础组织
        },
        // 组织信息列表
        lists: [],
        pinfo: {
          pid: '',
          pname: '',
          type: ''
        }
      }
    },
    computed: {},
    created() {
      this.setPList()
    },
    mounted() {
    },
    watch: {},
    methods: {
      // 打开
      openOrgDialog() {
        this.$emit('open', true)
      },
      // 关闭
      closeOrgDialog() {
        if (this.$refs.userSelect.tab == 'org') {
          this.$emit('close', true)
        } else {
          this.$refs.userSelect.tabBox('org')
        }
      },
      // 确认回调
      confirm(ev) {
        this.$emit('change', ev)
        this.$emit('confirm', true)
      },
      setPList() {
        if(this.customRoot.length){
          this.lists = this.dealData(this.customRoot, 'value', 'label')
          return
        }
        let userInfo = this.$A.GS('userInfo')
        let orgInfo = this.$A.GS('orgInfo')
        if (this.orgType == 'allOrg' || this.orgType == 'allOrgProject') {
          this.pinfo.pid = userInfo['orgId']
          this.pinfo.pname = userInfo['orgName']
          this.pinfo.type = userInfo['type']
        } else {
          this.pinfo.pid = orgInfo['orgId']
          this.pinfo.pname = orgInfo['orgName']
          this.pinfo.type = orgInfo['type']
        }
        if (this.pinfo.pid) {
          this.lists = [{
            label: this.pinfo.pname,
            value: this.pinfo.pid,
            type: this.pinfo.type,
            disabled: true,
            hasChecked: false,
            hasChildren: this.pinfo.type == '1' ? false : true ,
          }]
        } else {
          this.lists = []
        }

      },
      // 展开获取下级
      expand(item) {
        this.getOrgData(item)
      },
      unexpand(item) {
        if (item.value == 'org') {
          this.setPList()
        } else {
          this.getOrgData(item)
        }
      },
      // 获取组织信息
      getOrgData(item) {
        this.$A.Go('post', this.api.getOrgTreeDataApi, {
          "obj": {
            "organizationAbbreviation": "",
            "organizationCode": "",
            "organizationStatus": "",
            "organizationType": "",
            "parentOrganization": item.value,
            "tenantId": ""
          },
          "pageNo": -1,
          "pageSize": 10
        })
          .then(response => {
            Util.processRes(response, () => {
              if (this.orgType == 'allOrgProject' || this.orgType == 'currOrgProject') {
                // let projects = this.getProjects(response.body)
                this.lists = [...this.dealData(response.body, 'id', 'organizationName'),
                  ...(item.projects ? this.dealData(item.projects, 'id', 'projectName') : [])]
              } else {
                this.lists = this.dealData(response.body, 'id', 'organizationName')
              }
            });
            Util.processError(this, response);
          })
          .catch(err => {
            console.log(err)
          });
      },
      dealData(data, value = '', label = '') {
        let arr = []
        data.length && data.forEach((item) => {
          let obj = {}
          obj = item
          obj.value = item[value]
          obj.label = item[label]
          obj.type = item['type']
          if(obj.type == '1') {
            obj.hasChildren = false
          }else{
            obj.hasChildren = true
          }
          obj.hasChecked = false
          obj.disabled = true
          arr.push(obj)
        })
        return arr
      },
      getProjects(data) {
        let arr = []
        data.length && data.forEach((item) => {
          arr.push(item.projects)
        })
        return arr
      }
    },
    destroyed() {
    }
  }
</script>

<style lang="scss">
</style>
